layui.use(['table', 'admin', 'ax', 'laydate', 'form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var laydate = layui.laydate;
    var form = layui.form;

    laydate.render({
        elem:'#startTime'
        ,value:new Date()
    });

    laydate.render({
        elem:'#endTime'
        ,value:new Date()
    });
    /**
     * 库存表管理
     */
    var HxStorageStock = {
        tableId: "hxStorageStockTable"
    };

    /**
     * 初始化表格的列
     */
    HxStorageStock.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'vno', sort: true, title: '车牌号'},
            {field: 'adr', title: '车辆地理位置名称'},
            {field: 'utc', sort: true, title: '车辆定位时间'},
            {field: 'spd', sort: true, title: '速度'},
            {field: 'province', sort: true, title: '省'},
            {field: 'city', sort: true, title: '市'},
            {field: 'country', sort: true, title: '县'},
            {field: 'offlineState', sort: true, title: '离线状态', templet: function(d){
                    if (d.offlineState==false) {
                        return '在线';
                    } else  {
                        return '离线';
                    }
                }},
            {field: 'offlineTime', sort: true, title: '离线时长（单位/分钟）'},
            {field: 'createTime', sort: true, title: '创建时间'},
        ]];
    };
    // 渲染表格
    tableResult = table.render({
        elem: '#' + HxStorageStock.tableId,
        url: Feng.ctxPath + '/realLocation/list',
        page: true,
        limit: 60,
        limits:[60,120,240,360,480,600,2000,5000],
        height: "full-60",
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        totalRow: true ,//开启合计行
        cols: HxStorageStock.initColumn()
    });


    // 导入
    $('#btnExpimport').click(function () {
        layer.open({
            type: 2,
            title: '商品导入',
            area: ['1000px', '750px'],
            anim: 5,
            content: Feng.ctxPath + '/realLocation/toimportExcel1',
            end: function () {
                table.reload(HxStorageStock.tableId);
            }
        });
    });


    //批量删除
    $('#btnRevoke').click(function () {
        var checkStatus = table.checkStatus('hxStorageStockTable');
        data = checkStatus.data;
        if (data.length>0) {
            var ids = [];
            for (var i = 0; i < data.length; i++) {
                ids.push(data[i].id);
            }
            var operation = function () {
                var ajax = new $ax(Feng.ctxPath + "/realLocation/btnRevoke?ids="+ids, function (data) {
                    if(data.success){
                        Feng.success("删除成功!");
                        table.reload(HxStorageStock.tableId);
                    } else {
                        Feng.error("删除失败!" + data.message + "!");
                    }
                }, function (data) {
                    Feng.error("删除失败!" + data.responseJSON.message + "!");
                });
                ajax.start();
            };
            Feng.confirm("是否删除选中明细?", operation);
        } else {
            layer.alert('请至少选中一条数据');
        }
    });
    /**
     * 导出excel按钮
     */
    HxStorageStock.exportExcel = function () {
        var checkRows = table.checkStatus(HxStorageStock.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };
    // 导出excel
    $('#btnExp').click(function () {
        HxStorageStock.exportExcel();
    });


    /**
     * 点击查询按钮
     */
    HxStorageStock.search = function () {
        var queryData = {};
        queryData['startTime'] = $("#startTime").val();
        queryData['endTime'] = $("#endTime").val();
        queryData['vno'] = $("#condition").val();
        table.reload(HxStorageStock.tableId, {
            where: queryData, page: {curr: 1},
            done:function () {
                // 搜索按钮点击事件
                $('#btnSearch').click(function () {
                    HxStorageStock.search();
                });

            }
        });
    };

    $('#btnSearch').click(function () {
        HxStorageStock.search();
    });
    // 工具条点击事件
    table.on('tool(' + HxStorageStock.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            HxStorageStock.openEditDlg(data);
        } else if (layEvent === 'delete') {
            HxStorageStock.onDeleteItem(data);
        }
    });
});
